package com.zz.demo.entity;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Data
@Builder
@Slf4j
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {

    @ApiModelProperty("返回码")
    private int code;

    @ApiModelProperty("返回描述")
    private String message;

    @ApiModelProperty("返回数据")
    private T data;

    /**
     * 请求成功
     * @return
     */
    public static Result ok() {
        return result(HttpResult.SUCCESS.getCode(), HttpResult.FAIL.getMessage(), null);
    }

    public static <T> Result<T> ok(T data) {
        return result(HttpResult.SUCCESS, data);
    }

    /**
     * 请求失败
     *
     * @return
     */
    public static Result fail() {
        return result(HttpResult.FAIL.getCode(), HttpResult.SUCCESS.getMessage(), null);
    }
    public static Result fail(String message) {
        return result(HttpResult.FAIL.getCode(), message, null);
    }

    public static <T> Result<T> result(HttpResult httpResult, T data) {
        return result(httpResult.getCode(), httpResult.getMessage(), data);
    }

    public static <T> Result<T> result(Integer code, String message, T data) {
        return Result.<T>builder()
                .code(code)
                .message(message)
                .data(data)
                .build();
    }

    public void  error500(String message) {
        this.code = HttpResult.FAIL.getCode();
        this.message = message;
    }

    public void success(String message) {
        this.code = HttpResult.SUCCESS.getCode();
        this.message = message;
    }

    public void setResult(T data) {
        this.data = data;
    }

    /**
     * 传入的Boolean值，如果是true的，就把成功的值返回去，否则就是失败的值。
     * @param b 是否成功
     */
    public void setSuccess(Boolean b) {
        this.code = b? HttpResult.SUCCESS.getCode() : HttpResult.FAIL.getCode();
    }

}
